﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;
using Interactive.Utility.Extension;

namespace TravelLink.WebModel {
    public class ControlBase : System.Web.UI.UserControl {

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            if (DT > 0) {
                var publishDropdown = (this.FindControl("dropPublish") as DropDownList);
                if (publishDropdown != null) {
                    publishDropdown.SelectedValue = DT.ToString();
                }
            }
        }
        public TravelLink.DAL.TravelLinkDBDataContext DB {
            get {
                if (this.Page is Pagebase) {
                    return ((Pagebase)this.Page).DB;
                }
                return null;
            }
        }

        public OnlineUserInfo CurrentUser {
            get {
                if (this.Page is Pagebase) {
                    return (this.Page as Pagebase).CurrentUser;
                }
                return null;
            }
        }


        #region Helper Method

        private int _scriptFlag = 0;

        public void RegisteScript(string script) {
            if (this.Page is Pagebase) {
                (this.Page as Pagebase).RegisterScript(script);
            }
            else {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Script_" + _scriptFlag, script, true);
                _scriptFlag++;
            }
        }
        public string ToHtml(object str) {
            if (str == null) return string.Empty;
            return ToHtml(str.ToString());
        }

        public string ToHtml(string str) {
            if (string.IsNullOrEmpty(str)) return str;

            return str.Replace("\r", "<br/>");
        }
        public string Truncate(object str, int maxLength) {
            if (str == null) return string.Empty;
            return Truncate(str.ToString(), maxLength);
        }
        public string Truncate(string str, int maxLength) {
            if (string.IsNullOrEmpty(str)) return str;
            if (str.Length > maxLength) {
                return Interactive.Utility.Extension.StringExt.CutString(str, 0, maxLength) + "...";
            }
            else {
                return str;
            }
        }

        #endregion


        protected string EvalString(object obj) {
            if (obj == null) {
                return string.Empty;
            }
            else {
                return obj.ToString();
            }
        }

        public string EvalDatatime(object obj) {
            if (obj != null) {
                return ((DateTime)obj).ToString("yyyy-MM-dd");
            }
            else {
                return string.Empty;
            }
        }

        public string EvalShortDatatime(object obj) {
            if (obj != null) {
                return ((DateTime)obj).ToString("MM-dd");
            }
            else {
                return string.Empty;
            }
        }


        protected string GetPageJumpUrl(string queryName = "p") {
            if (HttpContext.Current.Request.QueryString.Keys.Count > 0) {
                string query = string.Empty;
                foreach (string item in HttpContext.Current.Request.QueryString.Keys) {
                    if (item == null) continue;
                    if (item.Equals(queryName, StringComparison.CurrentCultureIgnoreCase)) continue;

                    query += item.ToString() + "=" + HttpContext.Current.Request.QueryString[item.ToString()] + "&";
                }

                return HttpContext.Current.Request.Url.AbsolutePath + "?" + query;
            }
            return HttpContext.Current.Request.Url.AbsolutePath + "?";
        }



        #region Search Paramters
        //Position Level
        public int PL {
            get {
                return WebHelper.GetQueryStringInt("pl");
            }
        }


        //Position Type
        public int PT {
            get {
                return WebHelper.GetQueryStringInt("pt");
            }
        }


        //DataDate
        public int DT {
            get {
                return WebHelper.GetQueryStringInt("dt");
            }
        }

        //DataDate
        public int[] IST {
            get {
                string temp = WebHelper.GetQueryStringStr("ist");
                if (!string.IsNullOrEmpty(temp)) {
                    return temp.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(u => u.AsInt()).ToArray();
                }
                return new int[0];
            }
        }

        /// <summary>
        /// 城市ID
        /// </summary>
        public int CT {
            get {
                return WebHelper.GetQueryStringInt("ct");
            }
        }


        /// <summary>
        /// 城市名称
        /// </summary>
        public string CTN {
            get {
                return WebHelper.GetQueryStringStr("ctn");
            }
        }


        public string CPY {
            get {
                return WebHelper.GetQueryStringStr("cpy");
            }
        }


        #endregion
    }
}
